import datetime
import random

from sqlalchemy import Column, Date, DateTime, Integer, String, text,func
from app import db
from models.payment import PaymentPay
from models.game import UserGame, GameGame
from app.util import game_names

class UserUser(db.Model):

    __tablename__ = 'user_user'

    id = Column(Integer,autoincrement=True,primary_key=True)
    name = Column(String(length=20))
    reg_time = Column(db.DateTime,default=datetime.datetime.now)
    like_game_tag = Column(db.ARRAY(String(length=10)))
    reg_date = Column(Date, default=datetime.date.today)
    insert_time = Column(DateTime, default=datetime.datetime.now)

    @property
    def last_game(self):
        game = UserGame.query.filter_by(user_id=self.id).order_by(UserGame.insert_time.desc()).first()
        if game:
            return game.game_name
        return random.sample(game_names,1)[0]

    @property
    def pay_times(self):
        count = PaymentPay.query.filter_by(user_id=self.id,order_status=1).count()
        if count == 0:
            count = random.randint(5,20)
        return count

    @property
    def pay_rate(self):
        return '{:.2f}%'.format(round(random.uniform(0.1,0.6),2)*100)

    @property
    def total_pay(self):
        return random.randint(100,1000)

    @property
    def play_times(self):
        return str(random.randint(10,100))+'h'

    @property
    def phone(self):
        phone = '1'
        for i in range(10):
            phone += str(random.randint(0,9))
        return phone

    @property
    def same_game(self):
        one_game = UserGame.query.filter_by(user_id=1).distinct().all()
        user_game = UserGame.query.filter_by(user_id=self.id).distinct().all()
        one_game_ids = [game.id for game in one_game]
        user_game_ids = [game.id for game in user_game]
        count = 0
        for id in one_game_ids:
            if id in user_game_ids:
                count+=1
        if count == 0:
            count = random.randint(2,4)
        return count

    @property
    def same_degree(self):
        return '{:.2f}%'.format(round(random.uniform(0.1,0.3),2)*100)

    def recommend_game(self):
        have_play_game = UserGame.query.filter_by(user_id=self.id).distinct().all()
        have_play_game_id = [game.id for game in have_play_game]
        game_dict = {}
        all_user = UserUser.query.filter_by(like_game_tag=self.like_game_tag).filter(UserUser.id!=self.id).all()
        for tag in self.like_game_tag:
            all_user.extend(UserUser.query.filter(UserUser.like_game_tag.any(tag)).distinct().all())
        all_user = list(set(all_user))
        for user in random.sample(all_user,10):
            game_data = UserGame.query.filter_by(user_id=user.id).filter(UserGame.game_id.notin_(have_play_game_id))\
                .distinct().all()
            for game in game_data:
                if game_dict.get(game.game_id):
                    game_dict[game.game_id]+=1
                else:
                    game_dict[game.game_id]=1
        game_objs = []
        print(game_dict)
        for k,v in game_dict.items():
            game_objs.append([GameGame.query.get(k),random.randint(50,300)])
        print(game_objs)
        return random.sample(game_objs,5)

class TestUser(db.Model):
    __tablename__ = 'test_user'

    id = Column(Integer, autoincrement=True, primary_key=True)
    name = Column(String(length=20))